//
//  DoctorClass.swift
//  FiveProvincesOfNorthChina
//
//  Created by kys-20 on 2019/9/13.
//  Copyright © 2019 kys-20. All rights reserved.
//

import Foundation
import AVFoundation
import MobileCoreServices
import UIKit

protocol doctorClassroomProtocol:NSObjectProtocol {
    func celldidSelect(int:Int)
}

class DoctorClassroom: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource {
 
    
    
    
    weak var delegateClass :doctorClassroomProtocol?
    
    private var imageArray = [UIImage]()
    private var labelArray = [String]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.delegate = self
        self.dataSource = self
        self.backgroundColor = grayColor
        self.register(DoctorClassroomCell.self, forCellWithReuseIdentifier: "cell")
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! DoctorClassroomCell

        if imageArray.count != 0 && labelArray.count != 0{
            cell.setData(img: imageArray[indexPath.row], labelStr: labelArray[indexPath.row])
        }
        cell.backgroundColor = UIColor.white
        return cell
    }
    
    func setUpData(imageArray:[String],labelArray:[String]){
        asynchronousRequestImg(imageArray:imageArray)
        self.labelArray = labelArray
//        self.reloadData()
    }
    
    func asynchronousRequestImg(imageArray:[String]){

        if imageArray.count != 0 {
            for i in 0...imageArray.count-1{
            
                DispatchQueue.global().async {
                    //获取网络视频
                    let url = "\(domainName)\(imageArray[i])"
                    let videoURL = NSURL(string: url)!
                    let avAsset = AVURLAsset(url: videoURL as URL)
                    //生成视频截图
                    let generator = AVAssetImageGenerator(asset: avAsset)
                    generator.appliesPreferredTrackTransform = true
                    let time = CMTimeMakeWithSeconds(30.0,preferredTimescale: 600)
                    var actualTime:CMTime = CMTimeMake(value: 0,timescale: 0)
                    var imageRef:CGImage!
                    do{
                      imageRef = try generator.copyCGImage(at: time, actualTime: &actualTime)
                    }catch{
                        imageRef = UIImage(named: "only.jpg")?.cgImage
                    }
                       
                    let frameImg = UIImage(cgImage: imageRef)
                    DispatchQueue.main.async {
                        self.imageArray.append(frameImg)
                        self.reloadData()
                    }
                }
                
            } 
         
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.delegateClass?.celldidSelect(int: indexPath.row)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
